import pandas as pd
from datetime import datetime, timedelta
import re

def parse_gestational_age(weeks_str):
    """解析孕周字符串（如'11w+6'）为天数"""
    if pd.isna(weeks_str):
        return None
    # 提取数字部分
    parts = re.findall(r'\d+', str(weeks_str))
    if len(parts) >= 1:
        weeks = int(parts[0])
        days = int(parts[1]) if len(parts) > 1 else 0
        return weeks * 7 + days
    return None

def calculate_last_menstrual_date(detection_date, gestational_days):
    """根据检测日期和孕周计算末次月经日期"""
    if pd.isna(detection_date) or gestational_days is None:
        return None
    try:
        # 尝试不同的日期格式解析
        date_formats = ['%Y%m%d', '%Y-%m-%d', '%Y-%m-%d %H:%M:%S']
        detection_date_obj = None
        
        for fmt in date_formats:
            try:
                detection_date_obj = datetime.strptime(str(detection_date), fmt)
                break
            except ValueError:
                continue
                
        if detection_date_obj is None:
            return None
            
        # 正常妊娠期约为40周（280天），末次月经日期 = 检测日期 - 孕周天数
        last_menstrual = detection_date_obj - timedelta(days=gestational_days)
        return last_menstrual.strftime('%Y-%m-%d')
    except Exception as e:
        print(f"计算末次月经时出错: {e}")
        return None